package com.pearlsoft.webapp.rms.vappmgr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.VCloudException;
import com.vmware.vcloud.sdk.VM;
import com.vmware.vcloud.sdk.Vapp;
import com.vmware.vcloud.sdk.Vdc;

@QueryHandlerComponent(name = "VappMgrQueryHandler")
public class VappMgrQueryHandler extends AbsBaseQueryHandler{
	//组织的名字
	private String orgName = "test";
	private String vdcName = "test";
//	private String vappName = "vApp_administrator_2";
	
	private SimpleRecord changeVappToRecord(Vapp vapp,String vdcname){
		SimpleRecord recrod = new SimpleRecord();
		
	
			try {
				
				recrod.setValue("VAPP_VDC", vdcname);
				recrod.setValue("VAPP_NAME", vapp.getResource().getName());
				recrod.setValue("VAPP_DESCRIBE",vapp.getResource().getDescription());
				recrod.setValue("VAPP_STATUS", vapp.getVappStatus().name());
//				recrod.setValue("VAPP_SHARE", vapp.getResource());
				recrod.setValue("VAPP_VM",vapp.getChildrenVms().size());
				recrod.setValue("VAPP_OWNER", vapp.getResource().getOwner().getUser().getName());
//				recrod.setValue("VAPP_CREATED",);
//				recrod.setValue("VAPP_VDC", vapp.getVdcReference().getName());
				int cpu = 0;
		
				int memory = 0;
				for(VM vm:vapp.getChildrenVms()) 
				{
					cpu += vm.getCpu().getNoOfCpus();
					memory += Integer.parseInt(vm.getMemory().getMemorySize().toString());
				}
//				Vdc.getVdcByReference(vapp.getMetadata()., vdcRef);
				recrod.setValue("VAPP_CPU", cpu);
				recrod.setValue("VAPP_MEMORY", memory+" MB");
				recrod.setValue("VAPP_SIZE", vapp.getVappSize()+" MB");
			
			} catch (VCloudException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		

		return recrod;
		
	}
	

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handler(Query query, DataSet ds) throws Exception {
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
//		vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(orgName);
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
//		Vdc vdc = vdcs.get(query.get(vdcName));
		String vDC_Name = (String)query.get("VDC_Name");
		Vdc vdc = vdcs.get(vDC_Name);
	    String vdcname =  vdc.getResource().getName();
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		List<Record> list = new ArrayList<Record>();
		//Vapp vapp = vapps.get(vappName);
		for (Entry<String, Vapp> entry : vapps.entrySet()) {
			Vapp vapp = entry.getValue();
			Record record = changeVappToRecord(vapp,vdcname);
			list.add(record);
			
        }
		
		ds.setRecords(list);
	
		
		
	}

}
